package sub

import (
	"fmt"
	"quipus/cmd/qpc/sub"
	v1pb "quipus/proto/gen/api/v1"

	"github.com/spf13/cobra"
)

var (
	filter string
	limit  int32
	offset int32
)

func init() {
	// sub.RegisterCommonFlags(tenantCmd)

	listCmd.PersistentFlags().StringVarP(&filter, "filter", "", "", "filter tenant")
	listCmd.PersistentFlags().Int32VarP(&limit, "limit", "", 100, "list limit")
	listCmd.PersistentFlags().Int32VarP(&offset, "offset", "", 0, "list offset")

	resourceCmd.AddCommand(listCmd)
}

var listCmd = &cobra.Command{
	Use:   "list",
	Short: "Run cmd to list resource",
	RunE: func(cmd *cobra.Command, args []string) error {
		qconn, e := sub.NewGrpcConnect(sub.QpConfig.QPAddress)
		if e != nil {
			return e
		}

		qclient := v1pb.NewResourceServiceClient(qconn)
		ctxWithToken := sub.NewCtxWithToken(sub.QpConfig.Token)
		// pageToken := cpb.PageToken{Limit: 100, Offset: 0}
		in := &v1pb.ListResourcesRequest{}
		rsp, err := qclient.ListResources(ctxWithToken, in)
		if err != nil {
			return err
		}

		fmt.Println("URN              NAME           Type            Size ")
		if rsp != nil {
			for _, v := range rsp.Resources {
				fmt.Printf("%10s %10s    %12s         %10d  \n", v.Urn, v.Filename, v.Type, v.Size)
			}
		}
		// fmt.Print(rsp)
		return nil
	},
}
